<template>
    <div class="write-address">
        <nut-form>
            <nut-form-item label="姓名">
                <nut-input v-model="basicData.userName" class="nut-input-text" placeholder="请输入姓名" type="text" />
            </nut-form-item>
            <nut-form-item label="手机号">
                <nut-input v-model="basicData.userPhone" class="nut-input-text" placeholder="请输入手机号" type="text" />
            </nut-form-item>
            <nut-form-item label="地址">
                <nut-input v-model="basicData.userAddress" @click="chooseLocation" class="nut-input-text" disabled
                    placeholder="请选择地址" type="text" />
            </nut-form-item>
            <nut-form-item label="详细地址">
                <nut-textarea placeholder="请输入详细地址" type="text" v-model="basicData.userAddressInit" />
            </nut-form-item>
            <!-- <nut-form-item label="设为默认地址">
                <nut-switch v-model="basicData.defaultFlag"></nut-switch>
            </nut-form-item> -->
            <div class="del-address" @click="delAddr" v-if="basicData.ID">删除地址</div>
        </nut-form>
        <div class="save-Address">
            <nut-button shape="square" type="info" @click="saveAddress">保存</nut-button>
        </div>
    </div>
</template>

<script lang="ts">
import Taro, { useRouter } from '@tarojs/taro';
import { reactive, toRefs, onMounted } from 'vue';
import { addAddress, delAddress, putAddress, getAddress } from '../../../../api/index'
export default {
    components: {

    },
    setup() {
        const data = reactive({
            basicData: {
                ID: '',
                userName: '',
                userPhone: '',
                userAddressInit: '',
                userAddress: '',
                userLat: '' as any,
                userLng: '' as any,
            },
            addressId: '' as any,
            server_id: '',
            client_ID: '',
            server_User_Id: '',
        });
        const saveAddress = () => {
            const router = useRouter();
            let params = data.basicData
            delete params.ID
            if (router.params.addressId) {
                putAddress(params).then(res => {
                    if (res.code == 0) {
                        Taro.navigateTo({
                            url: '/packageA/pages/addressList/index?server_id=' + data.server_id + '&client_ID=' + data.client_ID + '&server_User_Id=' + data.server_User_Id
                        })
                    }
                })
            } else {
                addAddress(params).then(res => {
                    if (res.code == 0) {
                        Taro.navigateTo({
                            url: '/packageA/pages/addressList/index?server_id=' + data.server_id + '&client_ID=' + data.client_ID + '&server_User_Id=' + data.server_User_Id
                        })
                    }
                })
            }
        }
        onMounted(() => {
            const router = useRouter();
            if (router.params.addressId) {
                data.addressId = router.params.addressId
                getAddressDetail(router.params.addressId)
            }
            if (router.params.server_id) {
                data.server_id = router.params.server_id
            }
            if (router.params.server_User_Id) {
                data.server_User_Id = router.params.server_User_Id
            }
            if (router.params.client_ID) {
                data.client_ID = router.params.client_ID
            }
        });
        const getAddressDetail = (id) => {
            getAddress({ ID: id }).then(res => {
                if (res.code == 0) {
                    data.basicData = res.data.resysAddress
                }
            })
        }
        const delAddr = () => {
            delAddress(data.basicData.ID).then(res => {
                if (res.code == 0) {
                    Taro.showToast({
                        title: '删除成功',
                        icon: 'success',
                        duration: 2000
                    })
                    Taro.navigateTo({
                        url: '/packageA/pages/addressList/index'
                    })
                }
            })
        }
        const chooseLocation = () => {
            Taro.chooseLocation({
                success: function (res) {
                    data.basicData.userAddress = res.address
                    data.basicData.userLng = res.longitude + ''
                    data.basicData.userLat = res.latitude + ''
                },
                fail: function (res) {
                    console.log(res, '获取地址失败');
                },
            })
        }
        return {
            ...toRefs(data), saveAddress, getAddressDetail, delAddr, chooseLocation
        }
    }
};
</script>


<style lang="scss">
.write-address {
    .nut-cell {
        .nut-cell {
            padding: 0;
        }
    }

    .del-address {
        text-align: center;
        padding: 30rpx 0;
    }

}

.save-Address {
    position: fixed;
    bottom: 0;
    left: 0;
    background: #fff;
    padding: 20rpx 40rpx;
    box-sizing: border-box;
    width: 100%;
    text-align: center;

    .nut-button {
        width: 400rpx;
        margin: 0 auto;
        border-radius: 10rpx;
        background: #21C38F;
    }
}
</style>